package co.com.example;

import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.util.Map;
/**
 * <p>The NavigationBean class is responsible for storing the state of the
 * included dynamic content for display.  </p>
 */
@ManagedBean (name="navigation")
@SessionScoped
public class NavigationBean {

    // selected include contents.
    @ManagedProperty(value = "#{selectedIncludePath}")
    private String selectedIncludePath="content/splash.xhtml";

    /**
     * Gets the currently selected content include path.
     *
     * @return currently selected content include path.
     */
    public String getSelectedIncludePath() {
        //check for a currently selected path to be ready for ui:include
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        String requestedPath = (String) map.get("includePath");
        if ((null != requestedPath) && (requestedPath.length() > 0))  {
            selectedIncludePath = requestedPath;    
        }else selectedIncludePath="content/splash.xhtml";
        return selectedIncludePath;
    }

    /**
     * Sets the selected content include path to the specified path.
     *
     * @param selectedIncludePath the specified content include path.
     */
    public void setSelectedIncludePath(String selectedIncludePath) {
        this.selectedIncludePath = selectedIncludePath;
    }

    /**
     * Action Listener for the changes the selected content path.
     *
     * @param event JSF Action Event.
     */
    public void navigationPathChange(ActionEvent event){

        // Retrieve content include path from the context.
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        selectedIncludePath = (String) map.get("includePath");
    }

}
